﻿# Author: jumpingzp
# Email: jumpingzp@gmail.com

from django import forms
from meet001.commentboard.models import CommentBoard
from meet001.commentboard.models import Comment
from django.contrib.auth.models import User

class AddCommentForm(forms.Form):
  board_id = forms.CharField()
  content = forms.CharField(
    max_length = 1024)
  
# save comment data into database
# may raise exception: CommentBoard.DoesNotExist
  def save(self,author):
    if not self.is_valid():
      return
    cd = self.cleaned_data
    board = CommentBoard.objects.get(pk=cd['board_id'])
    if author is None or not author.is_authenticated():
      board.add_comment(cd['content'],None)
    else:
      board.add_comment(cd['content'],author)
    return
